package com.huabo.contract.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.hbfk.util.PageInfo;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.TblContractCollection;
import com.huabo.contract.oracle.entity.TblContractInvoicesmanagemen;
import com.huabo.contract.oracle.entity.TblContractPayment;
import com.huabo.contract.oracle.entity.TblContractPlannode;
import com.huabo.contract.oracle.entity.TblContractSpnode;
import com.huabo.contract.oracle.entity.TblCounterpartBankinfo;
import com.huabo.contract.oracle.entity.TblCyhwUnit;
import com.huabo.contract.oracle.entity.TblFlow;
import com.huabo.contract.oracle.entity.TblOrgBankaccount;
import com.huabo.contract.oracle.mapper.TblContractInvoicesmanagemenMapper;
import com.huabo.contract.oracle.mapper.TblContractPaymentMapper;
import com.huabo.contract.oracle.mapper.TblContractPlannodeMapper;
import com.huabo.contract.oracle.mapper.TblContractSpnodeMapper;
import com.huabo.contract.oracle.mapper.TblCounterpartBankinfoMapper;
import com.huabo.contract.oracle.mapper.TblCyhwUnitMapper;
import com.huabo.contract.oracle.mapper.TblFlowMapper;
import com.huabo.contract.oracle.mapper.TblOrgBankaccountMapper;
import com.huabo.contract.service.TblContractPaymenService;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class TblContractPaymenServiceImpl implements TblContractPaymenService {

    @Resource
    private TblContractPaymentMapper tblContractPaymentMapper;
    @Resource
    private TblFlowMapper tblFlowMapper;
    @Resource
    private TblContractSpnodeMapper tblContractSpnodeMapper;

    @Resource
    private TblContractPlannodeMapper tblContractPlannodeMapper;
    
    @Resource
    private TblCyhwUnitMapper tblCyhwUnitMapper;
    @Resource
    private TblCounterpartBankinfoMapper tblCounterpartBankinfoMapper;
    @Resource
    private TblContractInvoicesmanagemenMapper tblContractInvoicesmanagemenMapper;
    
    @Resource
    private  TblOrgBankaccountMapper tblOrgBankaccountMapper;
    
    
    @Override
    public Map<String,Object> findPaymentManagemenByPageInfo(PageInfo<TblContractPayment> pageInfo, String orgid, TblContractPayment payment, TblCyhwUnit unit,String flowid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblFlow flow = tblFlowMapper.findById(flowid);
            pageInfo.setTlist(this.tblContractPaymentMapper.selectPaymentManagemenByPageInfo(pageInfo, orgid, payment, unit));
            pageInfo.setTotalRecord(this.tblContractPaymentMapper.findPaymentManagemenByPageInfoCount(orgid, payment, unit));
            dataMap.put("pageInfo", pageInfo);
            dataMap.put("flow", flow);
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", dataMap);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblFlow flow = tblFlowMapper.findById(flowid);
            pageInfo.setTlist(this.tblContractPaymentMapper.selectPaymentManagemenByPageInfo(pageInfo, orgid, payment, unit));
            pageInfo.setTotalRecord(this.tblContractPaymentMapper.findPaymentManagemenByPageInfoCount(orgid, payment, unit));
            dataMap.put("pageInfo", pageInfo);
            dataMap.put("flow", flow);
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", dataMap);
            return resultMap;
        }
    }

    @Override
    public TblContractPayment selectInfoById(BigDecimal paymentid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return (TblContractPayment) this.tblContractPaymentMapper.selectPaymentid(paymentid);
        } else {
            return (TblContractPayment) this.tblContractPaymentMapper.selectPaymentid(paymentid);
        }

    }

    @Override
    public void updatePaymentInfo(TblContractPayment oldPayment) {
        this.tblContractPaymentMapper.updatePaymentInfo(oldPayment);
    }

    @Override
    public String savePaymentInfo(TblContractPayment payment) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblContractPaymentMapper.updatePaymentInfo(payment);
            return payment.getPaymentid().toString();
        } else {
            this.tblContractPaymentMapper.updatePaymentInfo(payment);
            return payment.getPaymentid().toString();
        }
    }

    @Override
    public TblContractPayment findPaymentInfoByParmentId(Integer paymentId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblContractPaymentMapper.findPaymentInfoByParmentId(paymentId);
        } else {
            return tblContractPaymentMapper.findPaymentInfoByParmentId(paymentId);
        }
    }

    @Override
    public void removePaymentInfo(Integer paymentId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblContractPaymentMapper.deletePaymentId(paymentId);
        } else {
            this.tblContractPaymentMapper.deletePaymentId(paymentId);
        }
    }

    @Override
    public TblContractPayment findByParmentId(BigDecimal parmentId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblContractPaymentMapper.findByParmentId(parmentId);
        } else {
            return tblContractPaymentMapper.findByParmentId(parmentId);
        }
    }


	@Override
	public String setData(JSONArray arr,String type) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (arr != null) {
                try {
                    for (Object j : arr) {
                        net.sf.json.JSONObject obj = (net.sf.json.JSONObject) j;
                        TblContractPayment payment = tblContractPaymentMapper.findByParment(obj.getString("zyx62"), obj.getString("zyx61"));
                        if(payment==null){
                        payment=new TblContractPayment();
                        payment.setPaymenttitle(obj.getString("collectionaccount") + obj.getString("total"));
                        payment.setPaymentlatedate(obj.get("paydate") != null ? sdf.parse(obj.get("paydate").toString()) : null);
                        payment.setBankaccount(obj.get("collectionaccount") != null ? obj.get("collectionaccount").toString() : null);
                        payment.setNodeid(new BigDecimal(obj.getString("zyx61")));
                        payment.setContractid(Integer.valueOf(obj.getString("zyx62")));
                        TblCyhwUnit  unit=tblCyhwUnitMapper.findContractById(payment.getContractid());
                        payment.setCreatestaff(unit.getCreateuser());
                        payment.setLinkorg(unit.getOrgid());
                        payment.setContractstatus(6);
                        payment.setBudgetid(unit.getContractxdfxinfo());
                        payment.setPaymentstatus(TblContractPayment.STATE_WC);
                        payment.setPaymenmoney(obj.get("total") != null ? new BigDecimal(obj.get("total").toString()) : new BigDecimal(0));
                      //获取合同相对方 并查询是否有该银行账号 否则新增 
                        TblCounterpartBankinfo bank= tblCounterpartBankinfoMapper.selectBankInfoById(unit.getContractxdfxinfo().toString(),payment.getBankaccount());
                        TblOrgBankaccount count= tblOrgBankaccountMapper.findByBankNum(obj.getString("collectionaccount"),unit.getOrgid());
                        if(bank==null){
                            bank=new TblCounterpartBankinfo();
                            bank.setBankaccount(obj.getString("collectionaccount"));
                            bank.setBankkhyh(obj.getString("collectionbank"));
                            bank.setBanknature(new BigDecimal(0));
                            bank.setBankstatus(new BigDecimal(1));
                            bank.setCreatestaff(unit.getCreateuser());
                            bank.setCreatetime(new Date());
                            bank.setBudgetid(unit.getContractxdfxinfo());
                            tblCounterpartBankinfoMapper.saveBank(bank);
                         }
                        if(count==null){
                        	count= new TblOrgBankaccount();
                            count.setBankaccname(obj.getString("collectionbank"));
                            count.setBankcode(obj.getString("collectionaccount"));
                            count.setBankaccnum(obj.getString("collectionaccount"));
                            count.setBankkhyh(obj.getString("collectionbank"));
                            count.setBankstate(new BigDecimal(0));
                            count.setBankstatus(new BigDecimal(1));
                            count.setOrgid(unit.getOrgid());
                            count.setCreatedate(new Date());
                            tblOrgBankaccountMapper.savebankInfo(count);
                        }
                         payment.setCounterbank(bank.getBankid());
                         payment.setOrgbank(count.getBankid());
                         TblContractInvoicesmanagemen men=new TblContractInvoicesmanagemen();
                         men.setInvoiceno((new Date()).getTime()+"");
                         men.setInvoicemoney(obj.get("total") != null ? new BigDecimal(obj.get("total").toString()) : new BigDecimal(0));
                         men.setInvoicedate(obj.get("paydate") != null ? sdf.parse(obj.get("paydate").toString()) : null);
//                       
//                        TblContractSpnode  spNode = new TblContractSpnode();
//                        spNode.setContractid(new BigDecimal(obj.getString("zyx62").toString()));
//                        spNode.setNodeid(new BigDecimal(obj.getString("zyx61").toString()));
//                         this.tblContractSpnodeMapper.saveBySpnode(spNode);
                        if (obj.getString("zyx61") != null) {
                            TblContractPlannode node = tblContractPlannodeMapper.findBynodeId(Integer.valueOf(obj.getString("zyx61")));
//                            node.setPlannodestatus(new BigDecimal("2"));
//                            tblContractPlannodeMapper.updateContractPlannode(node);
                            men.setInvoicepost(new BigDecimal(node.getNodepost()));
                            men.setInvoicecontent(node.getNodecontent());
                        }
                        tblContractInvoicesmanagemenMapper.saveMergenEntity(men);
                        payment.setInvoiceid(men.getInvoiceid());
                        tblContractPaymentMapper.savePaymenInfo(payment);
                       
                    }
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            } else {
                resultMap.put("code", "1");
                resultMap.put("msg", "失败！");
            }
            return resultMap.toString();
        } else {
            // TODO Auto-generated method stub
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (arr != null) {
                try {
                    // if(type.equals("BXD")){
                    for (Object j : arr) {
                        net.sf.json.JSONObject obj = (net.sf.json.JSONObject) j;
                        TblContractPayment payment = new TblContractPayment();
                        payment.setPaymenttitle(obj.getString("collectionaccount") + obj.getString("total"));
                        payment.setPaymentlatedate(obj.get("paydate") != null ? sdf.parse(obj.get("paydate").toString()) : null);
                        payment.setBankaccount(obj.get("collectionaccount") != null ? obj.get("collectionaccount").toString() : null);
                        payment.setNodeid(new BigDecimal(obj.getString("zyx2")));
                        payment.setContractid(Integer.valueOf(obj.getString("zyx1")));
                        payment.setContractstatus(6);
                        payment.setPaymenmoney(obj.get("total") != null ? new BigDecimal(obj.get("total").toString()) : new BigDecimal(0));
                        tblContractPaymentMapper.savePaymenInfo(payment);
                        //     获取到银行账户 根据合同关联的相对方ID和拿到的银行账户 去 银行账户表里面查询  没有就新增进去
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            } else {
                resultMap.put("code", "1");
                resultMap.put("msg", "失败！");
            }
            return resultMap.toString();
        }
	}

	@Override
	public String addPaymentInfo(TblContractPayment payment) {
		this.tblContractPaymentMapper.savePaymenInfo(payment);
        return payment.getPaymentid().toString();
	}

}
